package com.wang.controller;

import com.wang.entity.User;
import com.wang.service.UserService;
import com.wang.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Classname UserController
 * @Description TODO
 * @Date 2024/5/29 11:49
 * @Created by pc
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping(value = "/regUser")
    public String register(User user){
        user.setStatus(0);
        user.setCode(UUIDUtil.getUUID());
        userService.register(user);
        return "success";
    }

    /**
     * 校验邮箱中的code激活账户,
     * 首先根据激活码code查询用户，之后再把状态修改为"1"
     *
     * @param code
     * @return
     */
    @GetMapping(value = "/checkCode")
    public String checkCode(String code){
        User user = userService.checkCode(code);
        if(null != user){
            user.setStatus(1);
            user.setCode("");
            userService.updateUserStatus(user);
        }
        return "login";
    }

    /**
     * 跳转到登录页面
     *
     * @return
     */
    @GetMapping(value = "/loginPage")
    public String login() {
        return "login";
    }

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/loginUser")
    public String login(User user) {
        User u = userService.login(user);
        if (null != u) {
            return "welcome";
        }
        return "login";
    }
}
